/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentPrincipal.java
 *
 * Created on 09/04/2009, 16:25:25
 */

package interfaz;

import datos.Alumno;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import ordenar.*;
import persistencia.Register;
import persistencia.RegisterFile;

/**
 *
 * @author ivan
 */
public class VentPrincipal extends javax.swing.JFrame {

    private RegisterFile <Alumno>   datos;

    /** Creates new form VentPrincipal */
    public VentPrincipal() {       
        
        this.reset();
        datos=new RegisterFile("Ivan.java","rw",new Alumno());
        initComponents();
        rbSelectionSort.setSelected(true);
        btnReset.setEnabled(false);
        btnMostrar.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        rbSelectionSort = new javax.swing.JRadioButton();
        rbQuikSort = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        btnOrdenar = new javax.swing.JButton();
        btnMostrar = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        contenido = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Medoto de ordenamiento"));

        buttonGroup1.add(rbSelectionSort);
        rbSelectionSort.setText("SelectionSort");
        rbSelectionSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSelectionSortActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbQuikSort);
        rbQuikSort.setText("QuikSort");
        rbQuikSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbQuikSortActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("otro");

        btnOrdenar.setText("Ordenar");
        btnOrdenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrdenarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rbSelectionSort)
                            .addComponent(rbQuikSort)
                            .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(168, 168, 168)
                        .addComponent(btnOrdenar, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(404, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(rbSelectionSort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbQuikSort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addContainerGap(31, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(99, Short.MAX_VALUE)
                .addComponent(btnOrdenar))
        );

        btnMostrar.setText("Ver contenido Archivo");
        btnMostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMostrarActionPerformed(evt);
            }
        });

        btnReset.setText("Comenzar nueva prueba");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        contenido.setModel(new Modelo());
        jScrollPane1.setViewportView(contenido);

        jMenu1.setText("Archivo");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Salir");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                        .addGap(58, 58, 58)
                        .addComponent(btnMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(343, 343, 343))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnReset)
                    .addComponent(btnMostrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(133, 133, 133))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rbSelectionSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSelectionSortActionPerformed
        // TODO add your handling code here:
        datos.setTipoOrden(new SelectionSort());
        
}//GEN-LAST:event_rbSelectionSortActionPerformed

    private void btnMostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMostrarActionPerformed
        // TODO add your handling code here:        
        contenido.updateUI();
        btnMostrar.setEnabled(false);
}//GEN-LAST:event_btnMostrarActionPerformed

    private void btnOrdenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrdenarActionPerformed
        // TODO add your handling code here:
        if(datos.order())
            JOptionPane.showMessageDialog(this, "Confirmacion: El proceso de ordenamiento fue exitoso \n"+"Proceso de ordenamiento utilizado==>"+datos.getMethodOrder().toString());
        else
            JOptionPane.showMessageDialog(this, "Error: El proceso de ordenamiento no pudo efectuarse \n"+"Proceso de ordenamiento utilizado==>"+datos.getMethodOrder().toString());
        btnReset.setEnabled(true);
        this.btnMostrar.setEnabled(true);
        btnOrdenar.setEnabled(false);
}//GEN-LAST:event_btnOrdenarActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        datos.delete();
        reset();
        datos=new RegisterFile("Ivan.java","rw",new Alumno());
        rbSelectionSortActionPerformed(evt);
        rbSelectionSort.setSelected(true);
        contenido.updateUI();
        btnReset.setEnabled(false);
        btnOrdenar.setEnabled(true);
}//GEN-LAST:event_btnResetActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        datos.close();
    }//GEN-LAST:event_formWindowClosed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        datos.close();
        System.exit(0);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        datos.close();
    }//GEN-LAST:event_formWindowClosing

    private void rbQuikSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbQuikSortActionPerformed
        // TODO add your handling code here:
        datos.setTipoOrden(new QuikSort());
    }//GEN-LAST:event_rbQuikSortActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMostrar;
    private javax.swing.JButton btnOrdenar;
    private javax.swing.JButton btnReset;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTable contenido;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbQuikSort;
    private javax.swing.JRadioButton rbSelectionSort;
    // End of variables declaration//GEN-END:variables

    public void reset()
    {
        RegisterFile registro=new RegisterFile("Ivan.java","rw",new Alumno());
        Alumno[] alumnos={  new Alumno(71531,"Miguel",8f),
                            new Alumno(50000,"Antonio",7f),
                            new Alumno(65489,"Rosario",10f),
                            new Alumno(20154,"Tamara",9f),
                            new Alumno(75531,"Laura",8f),
                            new Alumno(21000,"Germa",6f),
                            new Alumno(66489,"Paola",10f),
                            new Alumno(89154,"Carolina",7f),
                            new Alumno(14531,"Pedro",8f),
                            new Alumno(38000,"Anotnela",7f),
                            new Alumno(95489,"Carlos",10f),
                            new Alumno(11154,"Jimena",9f),
                         };
        Register reg;
        for(int i=0;i<alumnos.length;i++){
            registro.append(alumnos[i]);
        }
        registro.close();
    }
    public class Modelo extends AbstractTableModel{

        public int getRowCount() {            
            return (int)datos.registerCount();
        }

        public int getColumnCount() {
            return 3;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Register r=new Register(new Alumno());
        datos.rewind();
        datos.seekRegister(rowIndex);
        datos.read(r);
        Alumno dato=(Alumno)r.getData();
            switch(columnIndex)
      {
        case 0:
          return dato.getLegajo();
        case 1:
          return dato.getNombre();
        case 2:
          return dato.getPromedio();
      }

       return null;
        }

        public String getColumnName(int column)
        {
            switch (column)
            {
                case 0: return "Legajo";
                case 1: return "Nombre";
                case 2: return "Promedio";
                default: return null;
            }

        }
    }
}
